﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IntroduceCompany.Singleton;
using IntroduceCompany.Util;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.Data.Linq;

namespace IntroduceCompany.Models
{
    public class ArticleListView_Validation
    {
        //[System.ComponentModel.DataAnnotations.MetadataType(typeof(IntroduceCompany.Models.ArticleListView_Validation))]  put in Article class of linq

        [Required(ErrorMessage = "Tiêu đề Không được để trống.")]
        public string Title { get; set; }

        [Required(ErrorMessage = "Mô tả Không được để trống.")]
        public string Description { get; set; }

        [Required(ErrorMessage = "Nội dung Không được để trống.")]
        public string Content { get; set; }
    }
    public class ArticleModels
    {
    #region BackEnd_Article
        /// <summary>
        /// get all article()
        /// </summary>
        /// <returns> IQueryable of ArticleListView  </returns>
        public IQueryable<ArticleListView> GetAllArticle(String languageID, String articleTypeID, String amount)
        {
            if (amount == "")
            {
                if (languageID == ConstantClass.markViewAllLanguage)
                {
                    if (articleTypeID == "")
                    {
                        return DataSingleton.GetInstance().ArticleListViews.OrderByDescending(u => u.ArticleID);
                    }
                    else
                    {
                        return DataSingleton.GetInstance().ArticleListViews.Where(u => u.ArticleTypeID == Convert.ToInt16(articleTypeID))
                            .OrderByDescending(u => u.ArticleID);
                    }
                }
                else
                {
                    if (articleTypeID == "")
                    {
                        return DataSingleton.GetInstance().ArticleListViews.Where(u => u.LanguageID == languageID).OrderByDescending(u => u.ArticleID);
                    }
                    else
                    {
                        return DataSingleton.GetInstance().ArticleListViews.Where(u => u.LanguageID == languageID && u.ArticleTypeID == Convert.ToInt16(articleTypeID))
                            .OrderByDescending(u => u.ArticleID);
                    }

                }
            }
            else 
            {
                int _amount = Convert.ToInt16(amount);
                if (languageID == ConstantClass.markViewAllLanguage)
                {
                    if (articleTypeID == "")
                    {
                        return DataSingleton.GetInstance().ArticleListViews.OrderByDescending(u => u.ArticleID).Take(_amount);
                    }
                    else
                    {
                        return DataSingleton.GetInstance().ArticleListViews.Where(u => u.ArticleTypeID == Convert.ToInt16(articleTypeID))
                            .OrderByDescending(u => u.ArticleID).Take(_amount);
                    }
                }
                else
                {
                    if (articleTypeID == "")
                    {
                        return DataSingleton.GetInstance().ArticleListViews.Where(u => u.LanguageID == languageID).OrderByDescending(u => u.ArticleID).Take(_amount);
                    }
                    else
                    {
                        return DataSingleton.GetInstance().ArticleListViews.Where(u => u.LanguageID == languageID && u.ArticleTypeID == Convert.ToInt16(articleTypeID))
                            .OrderByDescending(u => u.ArticleID).Take(_amount);
                    }

                }
            }
        }

        /// <summary>
        /// change the article visibility
        /// </summary>
        /// <param name="articleID"></param>
        /// <param name="articleVisible"></param>
        internal void ChangeArticleVisible(int articleID, bool articleVisible)
        {
            Article article = DataSingleton.GetInstance().Articles.SingleOrDefault(u => u.ArticleID == articleID);
            article.ArticleVisible = (!articleVisible);
            DataSingleton.GetInstance().Articles.Context.SubmitChanges();
        }

        /// <summary>
        /// get one article with full info in one language.
        /// </summary>
        /// <param name="articleID">articleID</param>
        /// <param name="languageID">languageID</param>
        /// <returns></returns>
        internal ArticleListView GetArticleByArticleIDAndLanguageID(int articleID, String languageID)
        {
            return DataSingleton.GetInstance().ArticleListViews.SingleOrDefault(u => u.ArticleID == articleID && u.LanguageID == languageID );
        }

        internal void ArticleEdit(ArticleListView _NewOneArticle)
        {
            Article _OldArticle = DataSingleton.GetInstance().Articles.SingleOrDefault(u => u.ArticleID == _NewOneArticle.ArticleID);

          //  _OldArticle.ArticleTypeID = _NewOneArticle.ArticleTypeID;
         //   _OldArticle.Photo = _NewOneArticle.Photo;
            _OldArticle.UserName = _NewOneArticle.Username;
            _OldArticle.ArticleDate = DateTime.Now;
            DataSingleton.GetInstance().Articles.Context.SubmitChanges();

            Article_L _OldArticle_L = DataSingleton.GetInstance().Article_Ls.SingleOrDefault(u => u.ArticleID == _NewOneArticle.ArticleID 
                && u.LanguageID == _NewOneArticle.LanguageID);

            _OldArticle_L.Title = _NewOneArticle.Title;
            _OldArticle_L.Description = _NewOneArticle.Description;
            _OldArticle_L.Content = _NewOneArticle.Content;

            DataSingleton.GetInstance().Article_Ls.Context.SubmitChanges();
            DataSingleton.GetInstance().Refresh(RefreshMode.KeepChanges, _OldArticle_L);
        }

        /// <summary>
        /// update the last one who edit the article.
        /// </summary>
        /// <param name="_NewOneArticleView"></param>
        //internal void UpdateArticleUserName(ArticleListView _NewOneArticle)
        //{
        //    Article _OldArticle = DataSingleton.GetInstance().Articles.SingleOrDefault(u => u.ArticleID == _NewOneArticle.ArticleID);

            
        //    _OldArticle.UserName = _NewOneArticle.Username;
        //    _OldArticle.ArticleDate = DateTime.Now;
        //    DataSingleton.GetInstance().Articles.Context.SubmitChanges();
        //}

        /// <summary>
        /// get one record from Artice table.
        /// </summary>
        /// <param name="articleID"></param>
        /// <returns></returns>
        internal Article GetArticleByArticleID(int articleID)
        {
            return DataSingleton.GetInstance().Articles.SingleOrDefault(u=>u.ArticleID == articleID);
        }

        internal void ArticleDelete(int articleID, String fullPath)
        {
            Article article = DataSingleton.GetInstance().Articles.SingleOrDefault(u => u.ArticleID == articleID);

            try
            {
                //Delete physical image 
                if (article.Photo != "")
                {
                    string LocalFile = fullPath +"/"+ article.Photo;
                    if (File.Exists(LocalFile))
                        File.Delete(LocalFile);
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); };

            DataSingleton.GetInstance().Articles.DeleteOnSubmit(article);
            DataSingleton.GetInstance().Articles.Context.SubmitChanges();
        }

        internal void Article_L_Create(ArticleListView _NewOneArticleView)
        {
            try
            {
                DataSingleton.GetInstance().sp_Article_L_Insert(_NewOneArticleView.ArticleID, _NewOneArticleView.LanguageID
                    , _NewOneArticleView.Title, _NewOneArticleView.Description, _NewOneArticleView.Content);
                // update time and usernam
                Article _OldArticle = DataSingleton.GetInstance().Articles.SingleOrDefault(u => u.ArticleID == _NewOneArticleView.ArticleID);
                _OldArticle.UserName = _NewOneArticleView.Username;
                _OldArticle.ArticleDate = DateTime.Now;
                DataSingleton.GetInstance().Articles.Context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                
            }
        }

        internal IQueryable<ArticleTypeView> GetArticleType(String languageID)
        {
            if (languageID == ConstantClass.markViewAllLanguage)
            {
                return DataSingleton.GetInstance().ArticleTypeViews.Where(u => u.LanguageID == ConstantClass.mainLanguageID);
            }
            else
            {
                return DataSingleton.GetInstance().ArticleTypeViews.Where(u => u.LanguageID == languageID);
            }
        }

        internal int ArticleCreate(ArticleListView _NewOneArticleView)
        {

            int re = DataSingleton.GetInstance().sp_ArticleInsert(_NewOneArticleView.ArticleTypeID, _NewOneArticleView.Photo, _NewOneArticleView.Username);


            return re;
        }

        /// <summary>
        /// update the new name of new thumbnail to sql database
        /// </summary>
        /// <param name="ArticleView"></param>
        internal void UpdateArticleThumbnail(ArticleListView ArticleView)
        {
            Article _OldArticle = DataSingleton.GetInstance().Articles.SingleOrDefault(u => u.ArticleID == ArticleView.ArticleID);

            _OldArticle.Photo = ArticleView.Photo;
            _OldArticle.UserName = ArticleView.Username;
            _OldArticle.ArticleDate = DateTime.Now;

            DataSingleton.GetInstance().Articles.Context.SubmitChanges();
        }

     #endregion


 ////////////////////////////////////////////////////////


     #region FrontEnd_Article

        public IQueryable<ArticleListView> Frontend_GetAllArticle(String languageID, String articleTypeID)
        {
            return DataSingleton.GetInstance().ArticleListViews.Where(u => u.LanguageID == languageID && u.ArticleTypeID == Convert.ToInt16(articleTypeID) 
                && u.ArticleVisible == true).OrderByDescending(u => u.ArticleID);
        }


        internal ArticleListView Frontend_GetArticleByArticleIDAndLanguageID(int articleID, String languageID)
        {
            return DataSingleton.GetInstance().ArticleListViews.SingleOrDefault(u => u.ArticleID == articleID && u.LanguageID == languageID && u.ArticleVisible == true);
        }

        /// <summary>
        /// get amount of ArticleListView for one page
        /// </summary>
        /// <param name="languageID"></param>
        /// <param name="articleTypeID"></param>
        /// <param name="currentPage"></param>
        /// <param name="step"></param>
        /// <returns></returns>
        internal IQueryable<ArticleListView> Frontend_GetOnePageArticle(string languageID, string articleTypeID, string currentPage, int step)
        {
            int startRowIndex = (Convert.ToInt16(currentPage) - 1) * step ;
            return DataSingleton.GetInstance().ArticleListViews.Where(u => u.ArticleTypeID == Convert.ToInt16(articleTypeID) && u.LanguageID == languageID 
                && u.ArticleVisible == true).OrderByDescending(u => u.ArticleID).Skip(startRowIndex).Take(step);
        }

        internal int Frontend_GetTotalRowsOneTypeOneLanguage(string articleTypeID, string languageID)
        {
            return DataSingleton.GetInstance().ArticleListViews.Where(u => u.ArticleTypeID == Convert.ToInt16(articleTypeID) && u.LanguageID == languageID 
                && u.ArticleVisible == true).Count();
        }

        internal bool CheckArticleSingle(int articleTypeID)
        {
            return DataSingleton.GetInstance().MenuLists.SingleOrDefault(u => u.MenuListID == articleTypeID).ArticleSingle;
        }

        /// <summary>
        /// get latest ArticleListView
        /// </summary>
        /// <param name="articleTypeID"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        internal ArticleListView Frontend_GetLatestArticles(int articleTypeID, string languageID)
        {
            return DataSingleton.GetInstance().ArticleListViews.Where(u => u.ArticleTypeID == articleTypeID && u.LanguageID == languageID 
                && u.ArticleVisible == true).OrderByDescending(u => u.ArticleID).FirstOrDefault();
        }

        /// <summary>
        /// get article ID by article SEO URL
        /// </summary>
        /// <param name="articleSEO_URL"></param>
        /// <returns></returns>
        internal int GetArticleIDBy_SEO_URL(string articleSEO_URL)
        {
            ArticleListView article_temp = DataSingleton.GetInstance().ArticleListViews.SingleOrDefault(u => u.ArticleSeoUrl == articleSEO_URL && u.ArticleVisible ==true);
            if(article_temp != null)
            {
                return article_temp.ArticleID;
            }
            return 0;
        }

        /// <summary>
        /// get article SEO URL by articleID and languageID
        /// </summary>
        /// <param name="articleID"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        internal string GetArticle_SEO_URL_By_ArticleID_LanguageID(int articleID, string languageID)
        {
            return DataSingleton.GetInstance().ArticleListViews.SingleOrDefault(u => u.ArticleID == articleID && u.LanguageID == languageID && u.ArticleVisible == true).ArticleSeoUrl;
        }

        /// <summary>
        /// check if a article has that language.
        /// </summary>
        /// <param name="articleID"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        internal bool CheckArticle_WithLanguage(int articleID, string languageID)
        {
            return DataSingleton.GetInstance().Article_Ls.Any(u => u.ArticleID == articleID && u.LanguageID == languageID);
        }
     #endregion








    }
}